//
//  Test.m
//  TestArea
//
//  Created by dlios on 15/6/3.
//  Copyright (c) 2015年 dlios. All rights reserved.
//

#import "Test.h"

@implementation Test
- (void)dealloc
{
    [_LatitudeLabel release];
    [_LongitudeLabel release];
    [_addressLabel release];
    [_locationManager release];
    [super dealloc];
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        self.locationManager.distanceFilter = 100; // 位置发生100米偏移时  重新定位
        [self.locationManager requestAlwaysAuthorization];
        [self.locationManager release];
    }
    return self;
}
- (void)startConnection
{
    [self.locationManager startUpdatingLocation];
}
#pragma mark -
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    switch (status) {
        case kCLAuthorizationStatusNotDetermined:
            
            if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
                
                [self.locationManager requestWhenInUseAuthorization];
                
            }
            break;
            
        default:
            break;
    }
    
    
}

#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    
    CLLocation *currLocation = [locations lastObject];
    self.LatitudeLabel.text = [NSString stringWithFormat:@"%f", currLocation.coordinate.latitude];
    self.LongitudeLabel.text = [NSString stringWithFormat:@"%f", currLocation.coordinate.longitude];
    
    NSLog(@"经度＝%f 纬度＝%f", currLocation.coordinate.latitude, currLocation.coordinate.longitude);
    // 关闭定位
    [self.locationManager stopUpdatingLocation];
    
    
    // 根据经纬度 反向编码  获取详细地址
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:currLocation
                   completionHandler:^(NSArray *placemarks, NSError *error){
                       for (CLPlacemark *place in placemarks) {
                           [self.delegate connection:place.locality];
//                           self.addressLabel.text = [NSString stringWithFormat:@"%@ %@ %@ %@ %@", place.country, place.subLocality, place.locality, place.subThoroughfare, place.thoroughfare];
//                           
                           NSLog(@"thoroughfare,%@",place.thoroughfare);       // 街道
                           NSLog(@"subThoroughfare,%@",place.subThoroughfare); // 子街道
                           NSLog(@"locality,%@",place.locality);               // 市
                           NSLog(@"subLocality,%@",place.subLocality);         // 区
                           NSLog(@"country,%@",place.country);                 // 国家
                       }
                   }];
}

@end
